﻿package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;

    public class BitmapExample extends Sprite {
        //private var url:String = "http://hengt.w201.west263.cn/zheng/image.jpg";
        private var url:String="http://www.cj888.com/star/u1/2006/07/13/20060713164135553673.jpg";
        private var size:uint = 200;

        public function BitmapExample() {
            configureAssets();
        }

        private function configureAssets():void {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            var request:URLRequest = new URLRequest(url);
            loader.x = size * numChildren;
            loader.load(request);
            addChild(loader);
        }

        private function duplicateImage(original:Bitmap):Bitmap {
            var image:Bitmap = new Bitmap(original.bitmapData.clone());
            image.x = size * numChildren;
            addChild(image);
            return image;
        }

        private function completeHandler(event:Event):void {
            var loader:Loader = Loader(event.target.loader);
            var image:Bitmap = Bitmap(loader.content);

            var duplicate:Bitmap = duplicateImage(image);
            var bitmapData:BitmapData = duplicate.bitmapData;
            var sourceRect:Rectangle = new Rectangle(0, 0, bitmapData.width, bitmapData.height);
            var destPoint:Point = new Point();
            var operation:String = ">";//下列比较运算符之一（作为字符串传递）:“<”、“<=”、“>”、“>=”、“==”“!=” 
            var threshold:uint = 0xFFFFccc;//测试每个像素时要比较的值，以查看该值是达到还是超过阈值。
            var color:uint = 0xffffff00;//阈值测试成功时对像素设置的颜色值。 默认值为 0x00000000。 
            var mask:uint = 0x000000ff;// 用于隔离颜色成分的遮罩。 
            var copySource:Boolean = true;//如果该值为 true，则源图像中的像素值将在阈值测试失败时复制到目标图像。 如果为 false，则在阈值测试失败时不会复制源图像。

            bitmapData.threshold(bitmapData,
                                 sourceRect,
                                 destPoint,
                                 operation,
                                 threshold,
                                 color,
                                 mask,
                                 copySource);
        }
        
        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("Unable to load image: " + url);
        }
    }
	
}